package com.shuai.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shuai.sql.SQL;
import com.shuai.stickynote.PlantNote;
import com.shuai.stickynote.R;
import com.shuai.tool.MyTime;

import java.util.ArrayList;

public class PlantListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<PlantNote> data;
    private SQL sql;

    public PlantListAdapter(Context context) {
        this.context = context;
        if (sql == null) sql = new SQL(context);
    }
    public void replaceAll(ArrayList<PlantNote> data){
        this.data = new ArrayList<>();
        this.data.clear();
        ArrayList<PlantNote> bData = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (MyTime.isDayBefortCurrentDay(data.get(i).getDay())) {
                data.get(i).setComplete(1);
                sql.updatePlant(data.get(i));
            } else {
                data.get(i).setComplete(0);
                sql.updatePlant(data.get(i));
                bData.add(data.get(i));
            }
        }
        if (bData != null && bData.size() > 0){
            this.data.addAll(bData);
        }
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.adapter_plant_list,null);
            holder = new ViewHolder();
            holder.tv_date = convertView.findViewById(R.id.adapter_plant_date);
            holder.tv_event = convertView.findViewById(R.id.adapter_plant_event);
            holder.tv_title = convertView.findViewById(R.id.adapter_plant_title);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (MyTime.toDateString(MyTime.getCurrentTimeOfStamp()).equals(MyTime.toDateString(data.get(position).getDay()))){
            holder.tv_date.setText("今天");
            holder.tv_date.setTextColor(Color.RED);
        }else {
            holder.tv_date.setText(MyTime.toDateString(data.get(position).getDay()));
            holder.tv_date.setTextColor(Color.BLACK);
        }
        if (data.get(position).getTitle().equals("")){
            holder.tv_title.setText("无标题");
            holder.tv_title.setTextColor(0xffcccccc);
        }else {
            holder.tv_title.setText(data.get(position).getTitle());
            holder.tv_title.setTextColor(0xff333333);
        }
        holder.tv_event.setText(data.get(position).getContent());
        return convertView;
    }

    static class ViewHolder{
        public TextView tv_date,tv_event,tv_title;
    }
}
